%%%-------------------------------------------------------------------
%%% File    : beta_node_sup.erl
%%% Author  :  vjache
%%% Description : 
%%%
%%% Created : 12 Jan 2010 by  vjache
%%%-------------------------------------------------------------------
-module(beta_node_sup).

-behaviour(supervisor).

%% --------------------------------------------------------------------
%% Include files
%% --------------------------------------------------------------------
-include_lib("eunit/include/eunit.hrl").
-include("../../include/erete.hrl").
-include("../../include/pipe.hrl").

%% API
-export([start_link/1]).

%% Supervisor callbacks
-export([init/1]).

-define(SERVER, ?MODULE).

%%====================================================================
%% API functions
%%====================================================================
%%--------------------------------------------------------------------
%% Function: start_link(Conf) -> {ok,Pid} | ignore | {error,Error}
%% Description: Starts the supervisor
%%--------------------------------------------------------------------
start_link(Conf) when ?IS_BETA_NODE_CONF_VALID(Conf) ->
    supervisor:start_link(
	  {local, supervisor_name(Conf#beta_node_conf.id)}, 
	  ?MODULE, 
	  [Conf]).

supervisor_name(BetaNodeId) when is_atom(BetaNodeId) ->
	list_to_atom(atom_to_list(BetaNodeId)++"sup").

%%====================================================================
%% Supervisor callbacks
%%====================================================================
%%--------------------------------------------------------------------
%% Func: init(Args) -> {ok,  {SupFlags,  [ChildSpec]}} |
%%                     ignore                          |
%%                     {error, Reason}
%% Description: Whenever a supervisor is started using 
%% supervisor:start_link/[2,3], this function is called by the new process 
%% to find out about restart strategy, maximum restart frequency and child 
%% specifications.
%%--------------------------------------------------------------------
init([Conf]) ->
    Activator = {activator,{beta_node_act_srv,start_link,[Conf]},
	      permanent,2000,worker,[beta_node_act_srv]},
	Receiver = {receiver,{beta_node_rcv_srv,start_link,[Conf]},
	      permanent,2000,worker,[beta_node_rcv_srv]},
    {ok,{{one_for_all,0,1}, [Activator,Receiver]}}.

%%====================================================================
%% Internal functions
%%====================================================================

%% --------------------------------------------------------------------
%%% eUnit test functions
%% --------------------------------------------------------------------
start_test() ->
	Conf=#beta_node_conf
		 {
		  id=BnId='bn#1', 								% an atom identifying beta_node server
		  in_ids=['an#1','an#2'],		 				% a list of atoms
		  out_pipe=pipe_console:new(), 					% an output pipe
		  beta_mem_fun=fun beta_memory_ets:open/1,		% beta memory factory fun -> #mh
		  red_fun=fun(_Modif,_K,IterList)->				% reduce function 
						  iter_join_cprod:all(IterList)
				  end,
		  red_mode = inc								% reduce mode (inc|aggr)
		 },
	{ok,_}=start_link(Conf),
	beta_node_rcv_srv:insert('an#1', BnId, [{k1,v1},{k1,v11},{k2,v2}]),
	beta_node_rcv_srv:insert('an#2', BnId, [{k1,v1a},{k1,v11a},{k2,v2a}]).

